//
//  StatisticViewController.m
//  CityFire
//
//  Created by Liuyz on 2017/7/31.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "StatisticViewController.h"
#import "CustomSegmentControlView.h"
#import <JHChart/JHColumnChart.h>

#import "WSDatePickerView.h"
#import "StatisticTableViewCell.h"
#import "StatisticViewModel.h"
#import "UIButton+ImageTitleCustom.h"
#import "SearchStatisticViewController.h"

//#define kStatisticArray     


@interface StatisticViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) IBOutlet CustomSegmentControlView *headerSegmentView;
@property (nonatomic, weak) IBOutlet CustomSegmentControlView *centerSegmentView;
@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;

@property (nonatomic, strong) JHColumnChart *columnChart;
@property (nonatomic, weak) IBOutlet UITableView   *tableView;
@property (nonatomic, weak) IBOutlet UIView        *chartView;

@property (nonatomic, weak) IBOutlet UILabel *coinLabel1;
@property (nonatomic, weak) IBOutlet UILabel *coinLabel2;
@property (nonatomic, weak) IBOutlet UILabel *coinLabel3;

@property (nonatomic, strong) StatisticViewModel *stisticViewModel;

@property (nonatomic, copy)   NSString *dayType;  //本周本日本月
@property (nonatomic, strong) UIButton *timeBtn;

@property (nonatomic, strong) NSMutableArray *statisticArray; //table数据源


@end

@implementation StatisticViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = kStatistic_TITLE;
    
    self.associateLength =1;
    self.loadingType=0;
    [self configUI];
    
    [self bindRAC];
    
    self.dayType = @"1";
    
    [self.stisticViewModel getWarnStatistic:@{@"daytype" : self.dayType ? : @"", @"start_time" : self.startTime ? : @"", @"end_time" : self.endTime ? : @""}];
    //    备注:username传空值不传代表查询所有
    //    时间段（daytype）:1 今天 2 本周 3 本月
    //    start_time,end_time为空时，按照daytype处理，不为空时，按照具体传入时间查询。
    
    [self.statisticArray addObjectsFromArray: @[@[@"alarm_coin", @"报警", @"0次"], @[@"fault_coin", @"故障", @"0次"], @[@"other_coin", @"其他", @"0次"]]];
}

- (void)showTimeSelect {
    SearchStatisticViewController *search = [[SearchStatisticViewController alloc] init];
    search.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:search animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.isFromSearchView) {
        [self.centerSegmentView selectTabIndex:0];
        DDLogInfo(@"StartDate :%@ EndDate:%@",self.startTime,self.endTime);
        [self.stisticViewModel getWarnStatistic:@{@"daytype" : self.dayType ? : @"", @"start_time" : self.startTime ? : @"", @"end_time" : self.endTime ? : @""}];
    }
}
- (void)bindRAC
{
    @weakify(self);
    [RACObserve(self.stisticViewModel, statisticModel) subscribeNext:^(StatisticModel *model) {
        @strongify(self);
        if (model) {
            [self.statisticArray removeAllObjects];
            
            [self.statisticArray addObjectsFromArray: @[@[@"alarm_coin", @"报警", [NSString stringWithFormat:@"%ld次", model.warntotalcount]], @[@"fault_coin", @"故障", [NSString stringWithFormat:@"%ld次", model.faulttotalcount]], @[@"other_coin", @"其他", [NSString stringWithFormat:@"%ld次", model.othertotalcount]]]];
            
            
            //            self.columnChart.xInfoTextArray = @[[NSString stringWithFormat:@"%ld次", model.warntotalcount], [NSString stringWithFormat:@"%ld次", model.faulttotalcount], [NSString stringWithFormat:@"%ld次", model.othertotalcount]];
            self.columnChart.valueArr = @[
                                          @[@(model.warnmiscount),@(model.warntotalcount), @(model.warnrightcount)],
                                          @[@(model.faultmiscount),@(model.faulttotalcount), @(model.faultrightcount)],
                                          @[@(model.othermiscount),@(model.othertotalcount), @(model.otherrightcount)],
                                          ];
            [self.columnChart showAnimation];
            
            
            [self.tableView reloadData];
        }
    }];
}
//"warntotalcount": 2,   --警告总数量
//"warnmiscount": 1,     --警告误报数量
//"warnrightcount": 1,   --警告正常数量
//"faulttotalcount": 2,  --故障总数量
//"faultmiscount": 1,    --故障误报数量
//"faultrightcount": 1,  --故障正常数量
//"othertotalcount": 0,  --其他总数量
//"othermiscount": 0,    --其他误报数量
//"otherrightcount": 0   --其他正常数量

/**
 设置UI
 */
- (void)configUI {
    self.timeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    self.timeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.timeBtn setImage:[UIImage imageNamed:@"time_coin"] forState:UIControlStateNormal];
    [self.timeBtn setTitle:@"选择日期" forState:UIControlStateNormal];
    [self.timeBtn imageOnTheTitleLeftWithSpace:3];
    [self.timeBtn addTarget:self action:@selector(showTimeSelect) forControlEvents:UIControlEventTouchUpInside];
    
    
    //把右侧的两个按钮添加到rightBarButtonItem
    UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:self.timeBtn];
    self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
    
    self.tableView.tableFooterView = [UIView new];
    
    self.coinLabel1.layer.cornerRadius = self.coinLabel1.frame.size.width / 2;
    self.coinLabel1.layer.masksToBounds = YES;
    self.coinLabel2.layer.cornerRadius = self.coinLabel1.frame.size.width / 2;
    self.coinLabel2.layer.masksToBounds = YES;
    self.coinLabel3.layer.cornerRadius = self.coinLabel1.frame.size.width / 2;
    self.coinLabel3.layer.masksToBounds = YES;
    
    
    self.headerSegmentView.titles = @[@"全部警报", @"正常报警", @"误报报警"];
    self.headerSegmentView.y = 7 ;
    self.headerSegmentView.width = 20 ;
    self.headerSegmentView.height = 15;
    self.headerSegmentView.cornerRadius = 3 ;
    
    [self.headerSegmentView setButtonOnClickBlock:^(NSInteger tag, NSString *title) {
        NSLog(@"index = %ld, title = %@", (long)tag, title);
    }];
    
    if (self.associateLength==1) {
        self.centerSegmentView.titles = @[@"区间",@"今天", @"本周", @"本月"];
    }else{
        self.centerSegmentView.titles = @[@"区间",@"今天", @"本周", @"本月"];
    }
    
    self.centerSegmentView.y = - 5 ;
    self.centerSegmentView.cornerRadius = 3 ;
    self.centerSegmentView.backgroundHeightLightColor = [UIColor colorWithRed:242 / 255.0 green:243 / 255.0 blue:245 / 255.0 alpha:1.0];
    self.centerSegmentView.titlesCustomeColor = [UIColor colorWithRed:109 / 255.0 green:125 / 255.0 blue:138 / 255.0 alpha:1.0];
    self.centerSegmentView.titlesHeightLightColor = [UIColor blackColor];
   
    [self.centerSegmentView setButtonOnClickBlock:^(NSInteger tag, NSString *title) {
        NSLog(@"index = %ld, title = %@", (long)tag, title);
        //        1区间 1今天 2 本周 3 本月
        if (tag==0) {
            NSLog(@"dayType %@",self.dayType);
            [self.stisticViewModel getWarnStatistic:@{@"daytype" : @"", @"start_time" : self.startTime ? : @"", @"end_time" : self.endTime ? : @""}];
        }
        else {
            self.dayType = [NSString stringWithFormat:@"%ld", tag];
            NSLog(@"dayType %@",self.dayType);
            [self.stisticViewModel getWarnStatistic:@{@"daytype" : self.dayType ? : @"", @"start_time" : @"", @"end_time": @""}];
        }
        
        
        
        
    }];
    
    
    
    
    /*        创建对象         */
    self.columnChart = [[JHColumnChart alloc] initWithFrame:CGRectMake(0, 0, self.chartView.frame.size.width, self.chartView.frame.size.height)];
    //    self..backgroundColor = [UIColor redColor];
    /*        创建数据源数组 每个数组即为一个模块数据 例如第一个数组可以表示某个班级的不同科目的平均成绩 下一个数组表示另外一个班级的不同科目的平均成绩         */
    [self.columnChart setNeedXandYLine:YES];
    [self.columnChart setIsShowYLine:NO];
    self.columnChart.xShowInfoText = @[@"报警",@"故障",@"其他"];
    /*        X、Y轴字体颜色         */
    self.columnChart.drawTextColorForX_Y = [UIColor lightGrayColor];
    /*        X、Y轴线条颜色         */
    self.columnChart.colorForXYLine = [UIColor blackColor];
    
    /*       该点 表示原点距左下角的距离         */
    self.columnChart.originSize = CGPointMake(30, 30);
    
    //    self.columnChart.typeSpace = 20;
    /*        第一个柱状图距原点的距离         */
    self.columnChart.drawFromOriginX = -15;
    /*        柱状图的宽度         */
    self.columnChart.columnWidth = 40;
    /*        每个模块的颜色数组 例如A班级的语文成绩颜色为红色 数学成绩颜色为绿色         */
    self.columnChart.columnBGcolorsArr = @[[UIColor colorWithRed: 132/ 255.0 green: 189/ 255.0 blue: 250/ 255.0 alpha:1.0],[UIColor colorWithRed: 55/ 255.0 green: 75/ 255.0 blue: 155/ 255.0 alpha:1.0],[UIColor colorWithRed: 172/ 255.0 green: 215/ 255.0 blue: 251/ 255.0 alpha:1.0]];
    /*        模块的提示语         */
    /*        开始动画         */
    [self.columnChart showAnimation];
    [self.chartView addSubview:self.columnChart];
    //    [self.scrollView addSubview:self.columnChart];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statisticArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StatisticTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StatisticTableViewCell"];
    
    if (cell != nil) {
        cell = [[StatisticTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"StatisticTableViewCell"];
    }
    
    cell.imageView.image = [UIImage imageNamed:self.statisticArray[indexPath.row][0]];
    cell.textLabel.text = self.statisticArray[indexPath.row][1];
    cell.detailTextLabel.text = self.statisticArray[indexPath.row][2];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (StatisticViewModel *)stisticViewModel
{
    if (_stisticViewModel == nil) {
        _stisticViewModel = [[StatisticViewModel alloc] init];
    }
    
    return _stisticViewModel;
}

- (NSMutableArray *)statisticArray
{
    if (_statisticArray == nil) {
        _statisticArray = [NSMutableArray array];
    }
    
    return  _statisticArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
